import Toast from "../../miniprogram_npm/@vant/weapp/toast/toast";
import Dialog from "../../miniprogram_npm/@vant/weapp/dialog/dialog";
import {
  ajaxGetUserAppletRoleList,
  ajaxDeletePowerUser
} from '../../api/power'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //自定义导航高度
    customNavHeight: app.globalData.customNavInfo.navHeight,
    baseURL: app.globalData.config.baseURL,
    list: [],
    // 当前编辑项index
    editIndex: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {

  },
  // 设置编辑项index
  setEditIndex({
    currentTarget: {
      dataset: {
        index
      }
    }
  }) {
    this.setData({
      editIndex: index
    })
  },
  // 清理编辑项index
  clearEditIndex() {
    this.setData({
      editIndex: null
    })
  },
  // 添加权限-前往搜索用户页面
  addUser() {
    app.commonNavigateTo('/pages/permissionManage/searchUser/index')
  },
  // 修改权限
  modifyPower({
    currentTarget: {
      dataset: {
        index
      }
    }
  }) {
    let item = this.data.list[index];
    // 前往权限编辑页面
    app.commonNavigateTo('/pages/permissionManage/editUser/index?type=modify', Object.assign({}, item, {
      userName: item.userName || item.userAlias,
      k1UserId: item.k1UserId || item.userId,
    }))
  },
  // 删除用户
  deleteUser({
    currentTarget: {
      dataset: {
        index
      }
    }
  }) {
    let item = this.data.list[index];
    Dialog.confirm({
        message: `下次授权需要重新编辑权限\n确定删除该权限用户吗？`,
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        confirmButtonColor: "#e61211",
        zIndex: 120,
      })
      .then(async () => {
        try {
          Toast.loading({
            message: '正在处理',
            forbidClick: true,
            duration: 0
          });
          await ajaxDeletePowerUser({
            userId: item.userId
          })
          this.data.list.splice(index, 1)
          this.setData({
            list: this.data.list
          })
          this.clearEditIndex();
          await app.myNextTick()
          Toast.clear();
        } catch (err) {
          await app.myNextTick()
          Toast.clear();
          Toast(err.msg || '删除失败');
        }
      })
      .catch(() => {});
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    Toast.loading({
      message: '正在加载',
      forbidClick: true,
      duration: 0
    });
    try {
      let {
        data
      } = await ajaxGetUserAppletRoleList();
      this.setData({
        list: (data || []).map(item => {
          item.powerList = [];
          if (item.ifCancel == 'Y') {
            item.powerList.push('核销');
          }
          if (item.ifAudit == 'Y') {
            item.powerList.push('退款');
          }
          item.powerStr = item.powerList.join('、')
          return item;
        })
      })
      await app.myNextTick()
      Toast.clear();
    } catch (err) {
      await app.myNextTick()
      Toast.clear();
      Toast(err.msg || '加载失败');
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  }
})